﻿using System.Collections.Generic;
using System.Reflection.Emit;
using System;
using Microsoft.EntityFrameworkCore;

namespace APMService
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {
        }
        public DbSet<APMGroup> APMGroups => Set<APMGroup>();
        public DbSet<APMContext> APMContexts => Set<APMContext>();


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<APMGroup>(entity =>
            {
                entity.ToTable("APMGroup");
                entity.HasIndex(e => e.Enable);
                entity.HasKey(e => e.Id);

            });

            modelBuilder.Entity<APMContext>(entity =>
            {
                entity.ToTable("APMContext");
                entity.HasKey(e => e.Id);
                entity.HasIndex(e => new { e.GroupId, e.Oridinal }).IsUnique();
                entity.HasIndex(e => e.GroupId);
                entity.HasIndex(e => e.ConditionNo);
                entity.HasOne(d => d.Group)
                    .WithMany(p => p.APMContexts)
                    .HasForeignKey(d => d.GroupId)
                    .OnDelete(DeleteBehavior.Cascade);

            });
        }
    }
}
